/******************
name:		kayla freeman
date:		8/1/24
purpose:	analysis (regs, tables, figs) using contract data
*******************/

clear

use  12_PreKData

keep if !missing(valid) /*note - valid is an indicator for whether receivables were mentioned as collateral, conditions, or eligibility criteria; 0 means i looked at the contract but didn't find anything; missing valid is when i couldn't see the k*/

/*
TABLE 4, FIRST LINE
*/
	tab valid

keep if valid==1
winsor2 ar_perc agg_ar_perc   s_hhi s_mkt_share s_ap_at s_ar_at s_ap_cogs s_ar_revt s_lnat s_q s_tang  s_profit c_hhi c_mkt_share c_ap_at c_ar_at c_ap_cogs s_age c_age aggmaj_prop ///
	chgcsale c_ar_revt c_lnat c_q c_tang  c_profit prop_sale ln_prop rel_length ln_numcust ln_numsupp   s_arS sp_arS     s_rdat c_rdat s_curdat c_curdat,replace

replace s_blev = 1 if s_blev>1
replace c_blev = 1 if c_blev>1
replace s_blev = 0 if s_blev<0
replace c_blev = 0 if c_blev<0

label var s_age "S Age"
label var c_age "C Age"
label var s_lnat "S Size"
label var c_lnat "C Size"
label var s_profit "S Profitability"
label var c_profit "C Profitability"
label var s_hhi "S HHI"
label var c_hhi "C HHI"
label var rel_length "Relationship Length"
label var sp_arS "Trade Credit"
label var ln_prop "Sales Dependence"
label var s_blev "S Leverage"
label var c_blev "C Leverage"


global svars   s_lnat s_blev s_profit s_hhi    s_age
global cvars   c_lnat  c_blev c_profit c_hhi    c_age
global relvars ln_prop  
global clustvar s_id  c_id


merge 1:1 s_gvkey c_gvkey year using 15_Kdata_Cust
drop if _merge==2
drop _merge

gen concvague = newconcvag
gen concrating = newconcrat  
	drop newconcvag newconcrat
label var concvag "I(vague exception), cust specific"
label var vagex "I(exceptions, but vague), supp-yr"

capture drop common2
gen common2 = commlen
	replace common2 = 0 if missing(commlen) & c_hasloan!=1


replace kcon_concperc = theconcperc if !missing(theconcperc) /*verifies that the concperc corresponding to the customer obs is the one used; only a few chgs*/

global clustvar s_id c_id
global intxclus  clustuses clustusec
 
 
/*define an "A/R Limit" var to be the presence of either a credit limit or a conc limit*/
gen kcon_ARlimit = 0 if kcon==1
	replace kcon_ARlimit = 1 if kcon_hasconc==1
	replace kcon_ARlimit = 1 if !missing(c_conclim)
	replace kcon_ARlimit = 1 if kcon_credlim==1
tab kcon_ARlimit

tab c_conclim
ren cconc c_except
capture drop cconc
gen cconc = 0 if kcon_ARlimit==1 |!missing(c_conclim) /*exists a few cases where there are specific customer limits without a blanket limit*/
	replace cconc= 1 if c_except==1
	tab concrating /*call invgr c's exceptions in these cases*/
	tab c_rating 
	gen ratelig = .
		replace ratelig = 1 if concrating==1 & (c_rating<"B" & !missing(c_rating)) 
		replace ratelig = 1 if concrating==1 & (c_rating=="BBB-" |c_rating=="BBB" | c_rating=="BBB+")
		
	replace c_conclim = ratingconclim if ratelig==1 & cconc!=1 /*13 chgs*/
	replace cconc = 1 if concrating==1 & ratelig==1 & cconc!=1 /*13 chgs*/
	
replace c_conclim = kcon_concperc if missing(c_conclim) & cconc!=1 & kcon_hasconc==1 /*fills in the c_conclim to be the general one if not included here*/
replace c_conclim = . if kcon_hasconc!=1


/*gap between cust-specific limit and blanket one*/
capture drop gap
gen gap = c_conclim-kcon_concperc
	replace gap = 0 if cconc!=1 & kcon_ARlimit==1
sum gap,det /*0 is the med, with 0s in the low group*/

/*estab good and bad conc*/	
capture drop goodconc
gen goodconc = cconc
	replace goodconc = 0 if gap<0
	replace goodconc = . if missing(gap) 
	
capture drop badconc
gen badconc = cconc
	replace badconc = 0 if gap>0 & gap<. & cconc==1
	replace badconc = . if missing(gap)

	/*alright. the tightness metrics. First, good exception = easycust*/

capture drop easycust
gen easycust = 0 if kcon_ARlimit==1
	replace easycust = 1 if cconc==1
	replace easycust = 0 if cconc==1 & badconc==1 /*not a positive exception if the cust limit is worse than blanket*/


/*gen the limprop var*/
capture drop limprop
gen limprop = c_conclim-prop_sale*100
sum limprop,det 


/*sales% - limit var*/
capture drop salesminlim
gen salesminlim = prop_sale*100-c_conclim
sum salesminlim,det 
sum salesminlim,det

/*TABLE 4, PANEL A (valid tabulation above, before dropping valid==0 for analysis) */
	/*next - within ar contract, conditions on ar*/
	tab kcon
	/*next - within condits, ar limit in form of cred limit or conc limit*/
	tab kcon_ARlimit
	tab easycust
	sum kcon
	
/*TABLE 4, PANEL B:*/
sum kcon_concperc c_conclim  salesminlim,det
capture drop overlim
gen overlim = ar_perc*100-c_conclim
sum overlim,det

/*************
TABLE 5 - VARIATION ALONG CONTRACT STRICTNESS
*************/	
	/*PANEL A*/
capture drop tight
gen tight = -(easycust-1)

capture drop use
gen use = tight

reghdfe sp_arS  $relvars  $cvars $svars  , absorb(year c_id s_id) vce(cluster $clustvar ),  if use==1  
eststo hasit
	qui estadd local sample "Low limit"
	qui estadd local sctrls "Yes"
	qui estadd local cctrls "Yes"
	qui estadd local csctrls "Yes"
	qui estadd local yrfe "Yes"
	qui estadd local cfe "Yes"
	qui estadd local sfe "Yes"
 reghdfe sp_arS  $relvars   $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ),  if use==0   
eststo doesnt
	qui estadd local sample "High limit"
	qui estadd local sctrls "Yes"
	qui estadd local cctrls "Yes"
	qui estadd local csctrls "Yes"
	qui estadd local yrfe "Yes"
	qui estadd local cfe "Yes"
	qui estadd local sfe "Yes"

capture drop bbb*
foreach var of varlist $relvars $svars $cvars  {
	gen bbb_`var' = use*`var'
}
capture drop clustuse*
egen clustuses = group(s_id  use)
egen clustusec = group(c_id use)
 reghdfe sp_arS  $relvars   $svars $cvars bbb* , absorb(year#use c_id#use s_id#use )vce(cluster $intxclus ),  
est store diff


capture drop tight
sum limprop,det
gen tight = 1 if limprop<=r(p50)
	replace tight = 0 if limprop >r(p50) & !missing(limprop)
capture drop use
gen use = tight

 reghdfe sp_arS  $relvars   $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ),  if use==1 
	eststo hasit2
	qui estadd local sample "Low limit"
	qui estadd local sctrls "Yes"
	qui estadd local cctrls "Yes"
	qui estadd local csctrls "Yes"
	qui estadd local yrfe "Yes"
	qui estadd local cfe "Yes"
	qui estadd local sfe "Yes"
 reghdfe sp_arS  $relvars   $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ),  if use==0
eststo doesnt2
	qui estadd local sample "High limit"
	qui estadd local sctrls "Yes"
	qui estadd local cctrls "Yes"
	qui estadd local csctrls "Yes"
	qui estadd local yrfe "Yes"
	qui estadd local cfe "Yes"
	qui estadd local sfe "Yes"

capture drop bbb*
foreach var of varlist $relvars $svars $cvars  {
	gen bbb_`var' = use*`var'
}
capture drop clustuse*
egen clustuses = group(s_id  use)
egen clustusec = group(c_id use)
 reghdfe sp_arS  $relvars   $svars $cvars bbb* , absorb(year#use c_id#use s_id#use )vce(cluster $intxclus ),  
eststo diff2

estout hasit doesnt diff hasit2 doesnt2 diff2 , order( ln_prop bbb_ln_prop ) label cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2_a N, labels("Adj. r-square" "N")) 


# delimit ;
esttab   hasit doesnt diff hasit2 doesnt2 diff2   ,  booktabs cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010)  stats(sample sctrls cctrls csctrls sfe cfe yrfe  r2_a N, fmt(%20s %20s %20s %20s %20s %20s %20s %9.3fc %9.0fc)label("Sample" "Supplier Characteristics" "Customer Characteristics" "Relationship Characteristics" "Supplier FE" "Customer FE"  "Year FE" "R2" "Observations"))  label 
	title("Borrowing Base Concentration Limits, Panel A")  collabels(none)   mtitles("Tight" "Lax" "Difference" "Tight" "Lax" "Difference"  )
	order( ln_prop bbb_ln_prop  ) keep(  ln_prop bbb_ln_prop) 
;
#delimit cr

	/*PANEL B*/
capture drop tight
gen tight = -(easycust-1)

capture drop use
gen use = tight
	replace use = 0 if missing(use) & (kcon_ARlimit==0 |kcon==0) 

reghdfe sp_arS  $relvars  $cvars $svars  , absorb(year c_id s_id) vce(cluster $clustvar ),  if use==1  
eststo hasit
	qui estadd local sample "Low limit"
	qui estadd local sctrls "Yes"
	qui estadd local cctrls "Yes"
	qui estadd local csctrls "Yes"
	qui estadd local yrfe "Yes"
	qui estadd local cfe "Yes"
	qui estadd local sfe "Yes"
 reghdfe sp_arS  $relvars   $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ),  if use==0   
eststo doesnt
	qui estadd local sample "High limit"
	qui estadd local sctrls "Yes"
	qui estadd local cctrls "Yes"
	qui estadd local csctrls "Yes"
	qui estadd local yrfe "Yes"
	qui estadd local cfe "Yes"
	qui estadd local sfe "Yes"

capture drop bbb*
foreach var of varlist $relvars $svars $cvars  {
	gen bbb_`var' = use*`var'
}
capture drop clustuse*
egen clustuses = group(s_id  use)
egen clustusec = group(c_id use)
 reghdfe sp_arS  $relvars   $svars $cvars bbb* , absorb(year#use c_id#use s_id#use )vce(cluster $intxclus ),  
est store diff


capture drop tight
sum limprop,det
gen tight = 1 if limprop<=r(p50)
	replace tight = 0 if limprop >r(p50) & !missing(limprop)
capture drop use
gen use = tight
	replace use = 0 if missing(use) & (kcon_ARlimit==0 |kcon==0)

 reghdfe sp_arS  $relvars   $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ),  if use==1 
	eststo hasit2
	qui estadd local sample "Low limit"
	qui estadd local sctrls "Yes"
	qui estadd local cctrls "Yes"
	qui estadd local csctrls "Yes"
	qui estadd local yrfe "Yes"
	qui estadd local cfe "Yes"
	qui estadd local sfe "Yes"
 reghdfe sp_arS  $relvars   $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ),  if use==0
eststo doesnt2
	qui estadd local sample "High limit"
	qui estadd local sctrls "Yes"
	qui estadd local cctrls "Yes"
	qui estadd local csctrls "Yes"
	qui estadd local yrfe "Yes"
	qui estadd local cfe "Yes"
	qui estadd local sfe "Yes"

capture drop bbb*
foreach var of varlist $relvars $svars $cvars  {
	gen bbb_`var' = use*`var'
}
capture drop clustuse*
egen clustuses = group(s_id  use)
egen clustusec = group(c_id use)
 reghdfe sp_arS  $relvars   $svars $cvars bbb* , absorb(year#use c_id#use s_id#use )vce(cluster $intxclus ),  
eststo diff2

estout hasit doesnt diff hasit2 doesnt2 diff2 , order( ln_prop bbb_ln_prop ) label cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2_a N, labels("Adj. r-square" "N")) 


# delimit ;
esttab   hasit doesnt diff hasit2 doesnt2 diff2   ,  booktabs cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010)  stats(sample sctrls cctrls csctrls sfe cfe yrfe  r2_a N, fmt(%20s %20s %20s %20s %20s %20s %20s %9.3fc %9.0fc)label("Sample" "Supplier Characteristics" "Customer Characteristics" "Relationship Characteristics" "Supplier FE" "Customer FE"  "Year FE" "R2" "Observations"))  label 
	title("Borrowing Base Concentration Limits, Panel B")  collabels(none)   mtitles("Tight" "Lax" "Difference" "Tight" "Lax" "Difference"  )
	order( ln_prop bbb_ln_prop  ) keep(  ln_prop bbb_ln_prop) 
;
#delimit cr

/*
TABLE IA-10: PROPENSITY TO BREAK CONCENTRATION LIMITS
*/
winsor2 s_cashat c_cashat,replace
capture drop hicash
gen hicash = 0 if kcon_hasconc==1
sum s_cashat,det , if kcon_hasconc==1
	replace hicash = 1 if s_cashat>r(p50) & kcon_hasconc==1
	replace hicash = . if missing(s_cashat)
	
capture drop hicms
gen hicms = 0 if kcon_hasconc==1
sum c_mkt_sh,det, if kcon_hasconc==1
	replace hicms = 1 if c_mkt_sh>r(p50) & kcon_hasconc==1

capture drop checkit bound	
gen checkit = 0
gen bound= ((ar_perc*100)-c_conclim	) 
	replace checkit=1 if bound>0 & !missing(bound)
	replace checkit = . if missing(bound)
	capture drop use
	gen use = !missing(cconc)
	capture drop use
	winsor2 bound,replace
	gen use = kcon_hasconc==1
	
label var hicash "I(High Cash)"

replace use =kcon_hasconc==1
  eststo check: reghdfe checkit      $relvars  $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ),   if use==1
	qui estadd local sctrls "Yes"
	qui estadd local cctrls "Yes"
	qui estadd local yrfe "Yes"
	qui estadd local cfe "Yes"
	qui estadd local sfe "Yes"
  eststo check2: reghdfe checkit    hicash  hicms $relvars   $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ),   if use==1
	qui estadd local sctrls "Yes"
	qui estadd local cctrls "Yes"
	qui estadd local yrfe "Yes"
	qui estadd local cfe "Yes"
	qui estadd local sfe "Yes"
  eststo check3: reghdfe checkit    hicash hicms cconc $relvars   $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ),   if use==1
	qui estadd local sctrls "Yes"
	qui estadd local cctrls "Yes"
	qui estadd local yrfe "Yes"
	qui estadd local cfe "Yes"
	qui estadd local sfe "Yes"
	
  eststo bounder: reghdfe bound    $relvars   $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ),   if use==1
	qui estadd local sctrls "Yes"
	qui estadd local cctrls "Yes"
	qui estadd local yrfe "Yes"
	qui estadd local cfe "Yes"
	qui estadd local sfe "Yes"
  eststo bounder2: reghdfe bound   hicash hicms  $relvars   $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ),   if use==1
	qui estadd local sctrls "Yes"
	qui estadd local cctrls "Yes"
	qui estadd local yrfe "Yes"
	qui estadd local cfe "Yes"
	qui estadd local sfe "Yes"
  eststo bounder3: reghdfe bound   hicash hicms cconc $relvars   $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ),   if use==1
	qui estadd local sctrls "Yes"
	qui estadd local cctrls "Yes"
	qui estadd local yrfe "Yes"
	qui estadd local cfe "Yes"
	qui estadd local sfe "Yes"
	
  eststo excep1: reghdfe cconc cinvgr  $relvars   $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ),   if use==1
	qui estadd local sctrls "Yes"
	qui estadd local cctrls "Yes"
	qui estadd local yrfe "Yes"
	qui estadd local cfe "Yes"
	qui estadd local sfe "Yes"
  eststo excep2: reghdfe cconc     common2  $relvars   $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ),   if use==1  
	qui estadd local sctrls "Yes"
	qui estadd local cctrls "Yes"
	qui estadd local yrfe "Yes"
	qui estadd local cfe "Yes"
	qui estadd local sfe "Yes"
  eststo excep3: reghdfe cconc        $relvars   $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ),   if use==1 
	qui estadd local sctrls "Yes"
	qui estadd local cctrls "Yes"
	qui estadd local yrfe "Yes"
	qui estadd local cfe "Yes"
	qui estadd local sfe "Yes"
  eststo excep4: reghdfe cconc cinvgr   common2  $relvars   $svars $cvars , absorb(year c_id s_id) vce(cluster $clustvar ),   if use==1 
	qui estadd local sctrls "Yes"
	qui estadd local cctrls "Yes"
	qui estadd local yrfe "Yes"
	qui estadd local cfe "Yes"
	qui estadd local sfe "Yes"


 
 estout check check2 check3 bounder bounder2 bounder3/*excep3 excep4*/, /*keep( $relvars $svars $cvars bbb_ln_prop) */order( ln_prop hicash hicms cconc) label cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2_a N, labels("Adj. r-square" "N")) 


#delimit ;
esttab check check2 check3 bounder bounder2 bounder3 ,append page booktabs cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010)  stats( sctrls cctrls  sfe cfe yrfe  r2_a N, fmt(%20s %20s %20s %20s %20s %9.3fc %9.0fc)label( "Supplier Characteristics" "Customer Characteristics" "Supplier FE" "Customer FE"  "Year FE" "R2" "Observations"))  label 
	title("Suppliers' Propensity to Break Concentration Limits")  collabels(none)   nomtitles 
	order( ln_prop  hicms hicash cconc   ) keep(  ln_prop hicash hicms cconc ) 
;
#delimit cr

/*FIGURE 3: DISTRIBUTION OF GENERIC AND CUST-SPECIFIC LIMITS*/
label var kcon_concperc "Generic Limit"
tab kcon_concperc /*min=5, max=50*/
levelsof kcon_concperc,local(levels)
    hist kcon_concperc ,discrete  freq  bcolor(black) barwidth(3) addlabels name(G1,replace) ylabel(,nolabels) xlabel(`levels') graphregion(color(white))
	graph export concperc.pdf,replace
label var c_conclim "Customer-Specific Limit"
tab c_conclim /*min=10, max=60*/
levelsof c_conclim,local(levels2)
    hist c_conclim ,discrete   freq   bcolor(gray) addlabels barwidth(3) name(G2,replace) ylabel(,nolabels) xlabel(`levels2')  graphregion(color(white))
graph export c_conlim.pdf,replace

tab gap
label var gap "Gap: Customer-Specific - Generic Limit"
levelsof gap,local(levels3)
    hist gap ,discrete  freq  bcolor(black) addlabels barwidth(3) name(G1,replace) ylabel(,nolabels) xlabel(`levels3')  graphregion(color(white))
	graph export gap.pdf,replace

	
	
/*
HERE - REVERT TO BROADER SAMPLE FOR MORE ANALYSIS
*/
clear
use  12_PreKData

keep if !missing(valid) /*note - valid is an indicator for whether receivables were mentioned as collateral, conditions, or eligibility criteria; 0 means i looked at the contract but didn't find anything; missing valid is when i couldn't see the k*/

tab year valid
winsor2 ar_perc s_hhi s_mkt_share s_ap_at s_ar_at s_ap_cogs s_ar_revt s_lnat s_q s_tang  s_profit c_hhi c_mkt_share c_ap_at c_ar_at c_ap_cogs s_age c_age aggmaj_prop ///
	chgcsale c_ar_revt c_lnat c_q c_tang  c_profit prop_sale ln_prop rel_length    sp_arS   s_rdat c_rdat s_curdat c_curdat,replace

replace s_blev = 1 if s_blev>1
replace c_blev = 1 if c_blev>1
replace s_blev = 0 if s_blev<0
replace c_blev = 0 if c_blev<0

label var s_age "S Age"
label var c_age "C Age"
label var s_lnat "S Size"
label var c_lnat "C Size"
label var s_profit "S Profitability"
label var c_profit "C Profitability"
label var s_hhi "S HHI"
label var c_hhi "C HHI"
label var rel_length "Relationship Length"
label var sp_arS "Trade Credit"
label var ln_prop "Sales Dependence"
label var s_blev "S Leverage"
label var c_blev "C Leverage"


global svars   s_lnat s_blev s_profit s_hhi    s_age
global cvars   c_lnat  c_blev c_profit c_hhi    c_age
global relvars ln_prop  
global clustvar s_id  c_id

merge 1:1 s_gvkey c_gvkey year using 15_Kdata_Cust
drop if _merge==2
drop _merge


gen concvague = newconcvag
gen concrating = newconcrat  
	drop newconcvag newconcrat
label var concvag "I(vague exception), cust specific"
label var vagex "I(exceptions, but vague), supp-yr"

capture drop common2
gen common2 = commlen
	replace common2 = 0 if missing(commlen) & c_hasloan!=1


replace kcon_concperc = theconcperc if !missing(theconcperc)  

global clustvar s_id c_id
global intxclus  clustuses clustusec
 
/*define an "A/R Limit" var to be the presence of either a credit limit or a conc limit*/
gen kcon_ARlimit = 0 if kcon==1
	replace kcon_ARlimit = 1 if kcon_hasconc==1
	replace kcon_ARlimit = 1 if !missing(c_conclim)
	replace kcon_ARlimit = 1 if kcon_credlim==1
tab kcon_ARlimit

tab c_conclim
ren cconc c_except
capture drop cconc
gen cconc = 0 if kcon_ARlimit==1 |!missing(c_conclim) /*exists a few cases where there are specific customer limits without a blanket limit*/
	replace cconc= 1 if c_except==1
	count if c_except==0 & !missing(kcon_concperc) & kcon_concperc!=c_conclim /*none; redoing the c_conclim file fixed this problem from earlier*/
	tab concvague
	tab concrating /*gonna call invgr c's exceptions in these cases*/
	tab c_rating 
	gen ratelig = .
		replace ratelig = 1 if concrating==1 & (c_rating<"B" & !missing(c_rating) )
		replace ratelig = 1 if concrating==1 & (c_rating=="BBB-" |c_rating=="BBB" | c_rating=="BBB+") 
		
	replace c_conclim = ratingconclim if ratelig==1 & cconc!=1 
	replace cconc = 1 if concrating==1 & ratelig==1 & cconc!=1 
	
	replace cconc = . if kcon_ARlimit!=1 
	tab cconc
	tab kcon_hasconc  

replace c_conclim = kcon_concperc if missing(c_conclim) & cconc!=1 & kcon_hasconc==1 /*fills in the c_conclim to be the general one if not included here*/
replace c_conclim = . if kcon_hasconc!=1


capture drop gap
gen gap = c_conclim-kcon_concperc
	replace gap = 0 if cconc!=1 & kcon_ARlimit==1
sum gap,det 


/*estab good and bad conc*/	
capture drop goodconc
gen goodconc = cconc
	replace goodconc = 0 if gap<0
	replace goodconc = . if missing(gap) 
	
	
capture drop badconc
gen badconc = cconc
	replace badconc = 0 if gap>0 & gap<. & cconc==1
	replace badconc = . if missing(gap)

tab goodconc badconc 

	/*alright. the tightness metrics. First, good exception = easycust*/

capture drop easycust
gen easycust = 0 if kcon_ARlimit==1
	replace easycust = 1 if cconc==1
	replace easycust = 0 if cconc==1 & badconc==1


/*gen the limprop var*/
capture drop limprop
gen limprop = c_conclim-prop_sale*100
sum limprop,det

sum kcon_concperc c_conclim gap limprop,det
capture drop overlim
gen overlim = ar_perc*100-c_conclim
sum overlim,det

capture drop wedge
gen wedge = (csale/s_revt)-(ar_amount/s_rect) 
replace wedge = wedge*100
sum wedge,det
	winsor2 wedge,replace

	
merge m:1 s_gvkey year using 11b_portdefaults_pkgdate
	drop if _m==2
	drop _m

capture drop use
gen use = 0 if defuse==1
capture drop hidef
gen hidef = 0 if !missing(pack_maxpercdef)
sum pack_maxpercdef,det, if  nolead==0  & def12mo!=1 &def24mo!=1
	replace hidef= 1 if pack_maxpercdef>=r(p75) & !missing(pack_maxpercdef)
	replace hidef = . if lasttwo>0&lasttwo<.

	
capture drop sfirst
bys s_gvk year: gen sfirst = _n==1
replace easycust = 0 if (kcon_hasconc==0 |missing(kcon_hasconc)) & !(kcon_hasconc==1 & missing(easycust))
replace kcon_hasconc = 0 if missing(kcon_hasconc)

/* define then winsor salesgr stuff*/
gen lead_firmsalesgr = (s_leadrevt)/s_revt -1
gen lead_avgconc = lead_custconc/lead_numcust
replace lead_avgconc = 0 if missing(lead_avgconc) & !missing(lead_firmsalesgr) /*no custs*/
winsor2 lead_avgconc lead_firmsalesgr leadsalesgr salesgr lead_custconc,replace

replace leadsalesgr =leadsalesgr*100 /*pct points*/

replace kcon_ARlimit = 0 if missing(kcon_ARlimit)
	
gen bind_limit = prop_sale*100-c_conclim
bys s_gvkey year: egen hascommon = max(common2)
capture drop mean_invgr
bys s_gvkey year: egen mean_invgr = mean(cinvgr)


/************
TABLE 6: FACTORS OF CONTRACT STRICTNESS
*************/
/*PANEL A: EXISTISTENCE OF AR LIMIT */
reghdfe kcon_ARlimit hidef       $svars  , absorb(year  s_id) vce(cluster s_id ), if sfirst /*more likely*/
	est store validdef
	qui estadd local yrfe "Yes"
	qui estadd local sfe "Yes"
reghdfe kcon_ARlimit mean_invgr       $svars  , absorb(year  s_id) vce(cluster s_id ),   if sfirst /*less likely*/
	est store validinv
	qui estadd local yrfe "Yes"
	qui estadd local sfe "Yes"
reghdfe kcon_ARlimit hascommon       $svars  , absorb(year  s_id) vce(cluster s_id ),   if sfirst /*no effect*/
	est store validcomm
	qui estadd local yrfe "Yes"
	qui estadd local sfe "Yes"

estout validdef validinv validcomm  , drop(_cons) order(hidef mean_invgr hascommon invgr_common)label cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2_a N, labels("Adj. r-square" "N")) 

# delimit ;
esttab  validdef validinv validcomm    ,  booktabs cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010)  stats( sfe yrfe r2_a N, fmt(%20s %20s  %9.3fc %9.0fc)label( "Firm FE"   "Year FE" "R2" "Observations"))  label 
	title("Existence of ARLimit")  collabels(none)  
	drop(_cons) order(hidef mean_invgr hascommon  )
;
#delimit cr

/*PANEL B: CUSTOMER EXCEPTION*/
/*exception within contracts with conc limits*/ 
reghdfe easycust hidef  ln_prop $cvars $svars  , absorb(year c_id s_id) vce(cluster $clustvar ),  if kcon_hasconc /*no effect*/
	est store validdef
	qui estadd local yrfe "Yes"
	qui estadd local sfe "Yes"
reghdfe easycust  cinvgr ln_prop $cvars $svars  , absorb(year c_id s_id) vce(cluster $clustvar ),  if kcon_hasconc  /*very positive*/
	est store validinv
	qui estadd local yrfe "Yes"
	qui estadd local sfe "Yes"
reghdfe easycust common2  ln_prop $cvars $svars  , absorb(year c_id s_id) vce(cluster $clustvar ),  if kcon_hasconc /*positive if investment grade*/
	est store validcomm
	qui estadd local yrfe "Yes"
	qui estadd local sfe "Yes"
estout validdef validinv validcomm  , drop(_cons) order(hidef cinvgr common2 c_invgrcommon)label cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2_a N, labels("Adj. r-square" "N")) 


# delimit ;
esttab  validdef validinv validcomm   ,  booktabs cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010)  stats( sfe yrfe r2_a N, fmt(%20s %20s  %9.3fc %9.0fc)label( "Firm FE"   "Year FE" "R2" "Observations"))  label 
	title("Table 6, panel b")  collabels(none)  
	drop(_cons) order(hidef cinvgr common2 )
;
#delimit cr


/************
TABLE 7: EFFECTS OF REC AUSTERITY ON CUST RELS
*************/
label var valid "A/R Contract"
label var kcon_ARlimit "A/R Limit"
reghdfe wedge kcon_ARlimit   ln_prop   $cvars $svars  , absorb(year c_id s_id) vce(cluster $clustvar ),  if    valid /*pos,sign*/
	eststo  limit
	qui estadd local schars "Yes"
	qui estadd local cchars "Yes"
	qui estadd local yrfe "Yes"
	qui estadd local sfe "Yes"
	qui estadd local cfe "Yes"

/*effect of wedge on...stuff*/
reghdfe cs_end wedge  ln_prop  $cvars $svars   , absorb(year c_id s_id) vce(cluster $clustvar ),  /*more likely to end*/  
  	eststo  endwedge
	qui estadd local schars "Yes"
	qui estadd local cchars "Yes"
	qui estadd local yrfe "Yes"
	qui estadd local sfe "Yes"
	qui estadd local cfe "Yes"

reghdfe leadsales wedge ln_prop  $cvars $svars  , absorb(year c_id s_id) vce(cluster $clustvar ),   /*much lower*/    
	eststo  salewedge
	qui estadd local schars "Yes"
	qui estadd local cchars "Yes"
	qui estadd local yrfe "Yes"
	qui estadd local sfe "Yes"
	qui estadd local cfe "Yes"

reghdfe cs_end bind_limit ln_prop $cvars $svars  , absorb(year c_id s_id) vce(cluster $clustvar ),   /*more likely to end*/    
	eststo  endbind
	qui estadd local schars "Yes"
	qui estadd local cchars "Yes"
	qui estadd local yrfe "Yes"
	qui estadd local sfe "Yes"
	qui estadd local cfe "Yes"
reghdfe leadsales bind_limit ln_prop $cvars $svars  , absorb(year c_id s_id) vce(cluster $clustvar ),   /* lower; stat insign with sample size*/    
	eststo salebind
	
	qui estadd local schars "Yes"
	qui estadd local cchars "Yes"
	qui estadd local yrfe "Yes"
	qui estadd local sfe "Yes"
	qui estadd local cfe "Yes"
estout   limit  endwedge salewedge endbind salebind, order(kcon_ARlimit wedge bind_limit) keep(kcon_ARlimit wedge bind_limit ln_prop) label cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2_a N, labels("Adj. r-square" "N")) 
# delimit ;
esttab  limit  endwedge salewedge endbind salebind  ,  booktabs cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010)  stats( sfe yrfe r2_a N, fmt(%20s %20s  %9.3fc %9.0fc)label( "Firm FE"   "Year FE" "R2" "Observations"))  label 
	title("Table 7")  collabels(none)  
 order(kcon_ARlimit wedge bind_limit) keep(kcon_ARlimit wedge bind_limit ln_prop)
;
#delimit cr

/*********
TABLE 8: FIRM OUTCOMES, 2SLS WITH HIDEF
*********/
reghdfe kcon_ARlimit   hidef     $svars  , absorb(year  s_id) vce(cluster s_id  ),  if  sfirst
	est store stage1
ivreghdfe lead_firmsalesgr (kcon_ARlimit =  hidef )    $svars  , absorb(year  s_id) cluster(s_id) ffirst first,  if  sfirst
	est store grow

ivreghdfe lead_avgconc (kcon_ARlimit =  hidef )    $svars  , absorb(year  s_id,savefe) cluster(s_id),  if  sfirst  
	est store concy
reghdfe kcon_ARlimit   hidef     $svars  , absorb(year  s_id) vce(cluster s_id  ),  if  sfirst & !missing(__hdfe1__)
	est store stage1

estout   stage1 grow concy, label cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010) stats(r2_a N, labels("Adj. r-square" "N")) 
# delimit ;
esttab  stage1 grow concy  ,  booktabs cells("b(star fmt(3))" t(par fmt(2))) starlevels( * 0.10 ** 0.05 *** 0.010)  stats( sfe cfe yrfe r2_a N, fmt(%20s %20s %20s %9.3fc %9.0fc)label( "Firm FE" "Customer FE"  "Year FE" "R2" "Observations"))  label 
	title("New stuff, panel C")  collabels(none)  
 order(hidef kcon_ARlimit) keep(hidef kcon_ARlimit)
;
#delimit cr


/*****
FIGURE 4: RECEIVABLE LIMITS AND REALIZED OUTCOMES
*****/

label var wedge "Sales% - A/R%"
label var leadsalesgr "Sales Growth"

gen prop100 = prop_sale*100
gen spar100 =sp_arS *100
gen arperc100 = ar_perc*100
sum arperc100,det

	/*PANEL A*/
binscatter   arperc100 c_conclim,discrete name(wed,replace) colors(black) lcolor(black) ytitle("Customer A/R%") xtitle("Customer-Specific Limit") xscale(range(0 65)) yscale(range(0 40)) xlabel(0(10)60) ylabel(0(10)35)
graph export lim_realized.pdf,replace
	/*PANEL B*/
binscatter   wedge bind_limit,name(wed,replace) colors(black) lcolor(black) yline(0) ytitle("TC Shortfall") xtitle("Sales% - Limit") xscale(range(-32 32)) yscale(range(-5 10)) xlabel(-30(5)30) ylabel(-5(5)10)
graph export wedge_realized.pdf,replace


/****
FIGURE 5: CUSTOMER SALES GROWTH AND K STRICTNESS
****/
binscatter leadsalesgr wedge ,name(wed,replace) colors(black) lcolor(black) xtitle("TC Shortfall") ytitle("Sales Growth") xscale(range(-40 40)) yscale(range(-10 30)) xlabel(-40(10)40) ylabel(-10(10)30)
graph export salesgr_wedge.pdf,replace

binscatter leadsalesgr bind_limit ,name(bin,replace) colors(black) lcolor(black) xtitle("Sales% - Limit") ytitle("Sales Growth") xscale(range(-40 40)) yscale(range(-10 30)) xlabel(-40(10)40) ylabel(-10(10)30)

graph export salesgr_bindlim.pdf,replace

